/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.stone.front.user.controller;

import com.stone.back.user.entity.BfCoupon;
import com.stone.back.user.entity.BfUser;
import com.stone.back.user.service.ICouponService;
import com.stone.base.controller.BaseController;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 优惠券
 * @author apple
 */
@Controller
public class CouponController extends BaseController {
    
    @Autowired
    private ICouponService couponService;
    
    /**
     * 查询优惠券
     * @param request
     * @return 
     */
    @RequestMapping(value = "/api/coupon", method = RequestMethod.GET)
    public @ResponseBody List<BfCoupon> queryCurrentUserCoupon(HttpServletRequest request) {
        BfUser current = this.getFrontCurrentUser(request);
        if(current == null)
            return Collections.emptyList();
        return couponService.queryByUserid(current.getUserid());
    }
    
    /**
     * 添加优惠劵
     * @param entity
     * @param request
     * @return 
     */
    @RequestMapping(value = "/api/coupon", method = RequestMethod.POST)
    public HttpEntity<String> updateCurrentUserCoupon(@RequestBody BfCoupon entity, HttpServletRequest request) {
        BfUser current = this.getFrontCurrentUser(request);
        if(current == null)
            return new ResponseEntity("未登录", HttpStatus.METHOD_FAILURE);
        couponService.addCoupon(current.getUserid(), entity.getCode());
        return new ResponseEntity(HttpStatus.OK);
    }
    
    /**
     * 删除优惠券
     * @param entity
     * @param request
     * @return 
     */
    @RequestMapping(value = "/api/coupon", method = RequestMethod.PUT)
    public HttpEntity<String> removeCurrentUserCoupon(@RequestBody BfCoupon entity, HttpServletRequest request) {
        BfUser current = this.getFrontCurrentUser(request);
        if(current == null)
            return new ResponseEntity("未登录", HttpStatus.METHOD_FAILURE);
        couponService.removeCoupan(current.getUserid(), entity.getId());
        return new ResponseEntity(HttpStatus.OK);
    }
    
}
